/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.logistics.service.customsdeclaration;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.financial.entity.declarationnotice.TdeclarationNotice;
import com.jeesite.modules.financial.service.declarationnotice.TdeclarationNoticeService;
import com.jeesite.modules.logistics.dao.customsdeclaration.CustomsDeclarationDao;
import com.jeesite.modules.logistics.dao.customsdeclaration.CustomsDeclarationDetailDao;
import com.jeesite.modules.logistics.entity.customsdeclaration.CustomsDeclaration;
import com.jeesite.modules.logistics.entity.customsdeclaration.CustomsDeclarationDetail;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 报关Service
 * @author LV
 * @version 2021-03-10
 */
@Service
@Transactional(readOnly=true)
public class CustomsDeclarationService extends CrudService<CustomsDeclarationDao, CustomsDeclaration> {

	@Autowired
	private CustomsDeclarationDetailDao customsDeclarationDetailDao;
	@Autowired
	private CodeRuleService codeRuleService;
    @Autowired
    private TdeclarationNoticeService declarationNoticeService;
    @Autowired
	private CustomsDeclarationDao customsDeclarationDao;

	/**
	 * 获取单条数据
	 * @param customsDeclaration
	 * @return
	 */
	@Override
	public CustomsDeclaration get(CustomsDeclaration customsDeclaration) {
		CustomsDeclaration entity = super.get(customsDeclaration);
		if (entity != null){
			CustomsDeclarationDetail customsDeclarationDetail = new CustomsDeclarationDetail(entity);
			customsDeclarationDetail.setStatus(CustomsDeclarationDetail.STATUS_NORMAL);
			entity.setCustomsDeclarationDetailList(customsDeclarationDetailDao.findList(customsDeclarationDetail));
		}
		return entity;
	}

	/**
	 * 查询分页数据
	 * @param customsDeclaration 查询条件
	 * @param customsDeclaration.page 分页对象
	 * @return
	 */
	@Override
	public Page<CustomsDeclaration> findPage(CustomsDeclaration customsDeclaration) {
		return super.findPage(customsDeclaration);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param customsDeclaration
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(CustomsDeclaration customsDeclaration) {
        if (customsDeclaration.getIsNewRecord()){
            customsDeclaration.setCustomsDeclarationCode(codeRuleService.getCode("customsdeclaration"));
        }
		super.save(customsDeclaration);
        if (customsDeclaration.getIsNewRecord()){
           codeRuleService.updateCurrentNumber("customsdeclaration");
        }

		// 保存上传图片
		FileUploadUtils.saveFileUpload(customsDeclaration.getId(), "customsDeclaration_image");
		// 保存上传附件
		FileUploadUtils.saveFileUpload(customsDeclaration.getId(), "customsDeclaration_file");
		// 保存 CustomsDeclaration子表
		for (CustomsDeclarationDetail customsDeclarationDetail : customsDeclaration.getCustomsDeclarationDetailList()){
			if (!CustomsDeclarationDetail.STATUS_DELETE.equals(customsDeclarationDetail.getStatus())){
				customsDeclarationDetail.setCustomsDeclarationId(customsDeclaration);
				if (customsDeclarationDetail.getIsNewRecord()){
					customsDeclarationDetailDao.insert(customsDeclarationDetail);
				}else{
					customsDeclarationDetailDao.update(customsDeclarationDetail);
				}
			}else{
				customsDeclarationDetailDao.delete(customsDeclarationDetail);
			}
		}

		// 回写报关通知单
        TdeclarationNotice declarationNotice = declarationNoticeService.get(customsDeclaration.getCustomsDeclarationNoticeCode());
		declarationNotice.setCustomsDeclarationCode(customsDeclaration.getCustomsDeclarationId());
        declarationNoticeService.save(declarationNotice);
    }

	/**
	 * 更新状态
	 * @param customsDeclaration
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(CustomsDeclaration customsDeclaration) {
		super.updateStatus(customsDeclaration);
	}

	/**
	 * 删除数据
	 * @param customsDeclaration
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(CustomsDeclaration customsDeclaration) {
		super.delete(customsDeclaration);
		CustomsDeclarationDetail customsDeclarationDetail = new CustomsDeclarationDetail();
		customsDeclarationDetail.setCustomsDeclarationId(customsDeclaration);
		customsDeclarationDetailDao.deleteByEntity(customsDeclarationDetail);
	}

	/**
	 * 查找车辆信息
	 * @param customsDeclaration
	 */
	@Transactional(readOnly=false)
	public List<CustomsDeclaration> findCarInfoList(CustomsDeclaration customsDeclaration) {
		return customsDeclarationDao.findCarInfoList(customsDeclaration);
	}
}
